package id.net.iconpln.bb.client.view;

import com.extjs.gxt.ui.client.Style.HorizontalAlignment;
import com.extjs.gxt.ui.client.Style.Orientation;
import com.extjs.gxt.ui.client.Style.SelectionMode;
import com.extjs.gxt.ui.client.widget.ContentPanel;
import com.extjs.gxt.ui.client.widget.LayoutContainer;
import com.extjs.gxt.ui.client.widget.MessageBox;
import com.extjs.gxt.ui.client.widget.button.Button;
import com.extjs.gxt.ui.client.widget.form.DateField;
import com.extjs.gxt.ui.client.widget.form.FieldSet;
import com.extjs.gxt.ui.client.widget.form.FormPanel;
import com.extjs.gxt.ui.client.widget.form.NumberField;
import com.extjs.gxt.ui.client.widget.form.SimpleComboBox;
import com.extjs.gxt.ui.client.widget.form.TextArea;
import com.extjs.gxt.ui.client.widget.form.TextField;
import com.extjs.gxt.ui.client.widget.form.ComboBox.TriggerAction;
import com.extjs.gxt.ui.client.widget.grid.ColumnConfig;
import com.extjs.gxt.ui.client.widget.grid.ColumnModel;
import com.extjs.gxt.ui.client.widget.grid.Grid;
import com.extjs.gxt.ui.client.widget.grid.RowNumberer;
import com.extjs.gxt.ui.client.widget.layout.FitData;
import com.extjs.gxt.ui.client.widget.layout.FitLayout;
import com.extjs.gxt.ui.client.widget.layout.FormData;
import com.extjs.gxt.ui.client.widget.layout.FormLayout;
import com.extjs.gxt.ui.client.widget.layout.RowData;
import com.extjs.gxt.ui.client.widget.layout.RowLayout;
import com.google.gwt.i18n.client.DateTimeFormat;
import com.google.gwt.user.client.Element;
import com.extjs.gxt.ui.client.binding.FormBinding;
import com.extjs.gxt.ui.client.data.ModelData;
import com.extjs.gxt.ui.client.event.ButtonEvent;
import com.extjs.gxt.ui.client.event.Events;
import com.extjs.gxt.ui.client.event.Listener;
import com.extjs.gxt.ui.client.event.MessageBoxEvent;
import com.extjs.gxt.ui.client.event.SelectionChangedEvent;
import com.extjs.gxt.ui.client.event.SelectionListener;
import com.extjs.gxt.ui.client.store.ListStore; 
import id.net.iconpln.bb.client.data.TestData;
import id.net.iconpln.bb.client.model.PageBbDokBean;
import id.net.iconpln.bb.client.model.PageBbStockBean;
import id.net.iconpln.bb.client.model.Stock;
import java.util.ArrayList;  
import java.util.List;  

public class PageBbDok extends LayoutContainer {
	
	Grid<PageBbDokBean> grid;
	private FormData formData;
	private FormBinding formBindings; 
	
	@Override
	  protected void onRender(Element parent, int pos) {
	    super.onRender(parent, pos); 
	    setLayout(new FitLayout());   
	    
	    ContentPanel cp = new ContentPanel();  
	  
	    cp.setHeading("Dokumen Kontrak");  
	    cp.setFrame(true);  
	    cp.setLayout(new RowLayout(Orientation.HORIZONTAL));  
	  
	    grid = createGrid();
	    grid.getSelectionModel().setSelectionMode(SelectionMode.SINGLE);
	    grid.getSelectionModel().addListener(Events.SelectionChange,  
	            new Listener<SelectionChangedEvent<PageBbDokBean>>() {  
	              public void handleEvent(SelectionChangedEvent<PageBbDokBean> be) {  
	                if (be.getSelection().size() > 0) {  
	                  formBindings.bind((ModelData) be.getSelection().get(0));  
	                } else {  
	                  formBindings.unbind();  
	                }  
	              }  
	            });  
	    FormPanel panel = createForm();
	    formBindings = new FormBinding(panel, true);  
	  
	    cp.add(panel, new RowData(.4, 1));  
	    cp.add(grid, new RowData(.6, 1)); 
	    add(cp, new FitData(5));  
	  }  
	  
	  private FormPanel createForm() {  
	    FormPanel panel = new FormPanel();  
	    panel.setHeaderVisible(false);
	    
	    FieldSet fieldSet2 = new FieldSet();  
	    fieldSet2.setHeading("Data kontrak pemasok");  
	    fieldSet2.setCollapsible(false);

	    FormLayout layout2 = new FormLayout();  
	    layout2.setLabelWidth(150);  
	    fieldSet2.setLayout(layout2);
	    
	    final SimpleComboBox<String> txtnopemasok = new SimpleComboBox<String>();  
	    txtnopemasok.setFieldLabel("No Pemasok");  
	    txtnopemasok.setForceSelection(true);  
	    txtnopemasok.setTypeAhead(true);   
	    txtnopemasok.setTriggerAction(TriggerAction.ALL);
	    txtnopemasok.add("001");
	    txtnopemasok.add("002");
	    txtnopemasok.add("003");
	    fieldSet2.add(txtnopemasok, formData);
	    
	    final TextField<String> txtnama_pemasok = new TextField<String>();
	    txtnama_pemasok.setName("nama_pemasok");
	    txtnama_pemasok.setFieldLabel("Nama Pemasok");  
	    fieldSet2.add(txtnama_pemasok, formData); 
	    
	    final TextField<String> txtno_kontrak = new TextField<String>();
	    txtno_kontrak.setName("no_kontrak");
	    txtno_kontrak.setFieldLabel("No Kontrak");  
	    fieldSet2.add(txtno_kontrak, formData);  
	  
	    final TextField<String> txtlama_kontrak = new TextField<String>();
	    txtlama_kontrak.setName("lama_kontrak");
	    txtlama_kontrak.setFieldLabel("Lama Kontrak");  
	    fieldSet2.add(txtlama_kontrak, formData);
	    
	    DateTimeFormat dtFormat = DateTimeFormat.getFormat("d/M/yy h:mma");
	    final DateField txttgl_kontrak = new DateField();
	    txttgl_kontrak.setName("Tglkontrak");
	    txttgl_kontrak.setFieldLabel("Tanggal Kontrak");
	    txttgl_kontrak.getPropertyEditor().setFormat(dtFormat);
	    fieldSet2.add(txttgl_kontrak, formData);
	    
	    final DateField txthabis_kontrak = new DateField();
	    txthabis_kontrak.setName("habis_kontrak");
	    txthabis_kontrak.setFieldLabel("Habis Kontrak");
	    txthabis_kontrak.getPropertyEditor().setFormat(dtFormat);
	    fieldSet2.add(txthabis_kontrak, formData);
	    
	    final TextField<String> txtkalori_bb = new TextField<String>();
	    txtkalori_bb.setName("kalori_bb");
	    txtkalori_bb.setFieldLabel("Kalori BB");  
	    fieldSet2.add(txtkalori_bb, formData);
	    
	    final TextArea txtalamat_pemasok = new TextArea();
	    txtalamat_pemasok.setName("alamat_pemasok");
	    txtalamat_pemasok.setFieldLabel("Alamat Pemasok");  
	    fieldSet2.add(txtalamat_pemasok, formData);
	    
	    panel.add(fieldSet2);
	    
	    ContentPanel p = new ContentPanel();
	    p.setHeaderVisible(false);	    
	    p.setButtonAlign(HorizontalAlignment.RIGHT);  
	    p.addButton(new Button("Reset", new SelectionListener<ButtonEvent>() {  
	      @Override  
	      public void componentSelected(ButtonEvent ce) {  
	        grid.getStore().rejectChanges();
	        txtkalori_bb.setValue("");
	        txtalamat_pemasok.setValue("");
	        txtnama_pemasok.setValue("");
	        txtno_kontrak.setValue("");
	        txtlama_kontrak.setValue("");
	        txttgl_kontrak.setValue(null);
	        txthabis_kontrak.setValue(null);
	      }  
	    }));  
	    p.addButton(new Button("Submit", new SelectionListener<ButtonEvent>() {  
	      @Override  
	      public void componentSelected(ButtonEvent ce) {  
	        grid.getStore().commitChanges(); 
	        MessageBox.alert("Message", "DATABASE sedang dalam Perbaikan", null);
	      }  
	    }));  

	    panel.add(p);
	    return panel;  
	  }  
	  
	  private Grid<PageBbDokBean> createGrid() {  
	   
		final List<ColumnConfig> columns = new ArrayList<ColumnConfig>(); 
	    
		RowNumberer id = new RowNumberer();  
	    id.setWidth(30);
	    
	    ColumnConfig nama_pemasok = new ColumnConfig("nama_pemasok", "Nama Pemasok", 120);
	    nama_pemasok.setAlignment(HorizontalAlignment.LEFT);
	    
	    ColumnConfig no_kontrak = new ColumnConfig("no_kontrak", "No Kontrak", 250);
	    no_kontrak.setAlignment(HorizontalAlignment.LEFT);
	    
	    ColumnConfig lama_kontrak = new ColumnConfig("lama_kontrak", "Lama Kontrak", 100);
	    lama_kontrak.setAlignment(HorizontalAlignment.RIGHT);
	    
	    ColumnConfig tglkontrak = new ColumnConfig("Tglkontrak", "Tanggal Kontrak", 130);
	    tglkontrak.setAlignment(HorizontalAlignment.RIGHT);
	    tglkontrak.setDateTimeFormat(DateTimeFormat.getShortDateTimeFormat());
	    
	    ColumnConfig habis_kontrak = new ColumnConfig("habis_kontrak", "Habis Kontrak", 130);
	    habis_kontrak.setAlignment(HorizontalAlignment.RIGHT);
	    habis_kontrak.setDateTimeFormat(DateTimeFormat.getShortDateTimeFormat());
	    
	    ColumnConfig kalori_bb = new ColumnConfig("kalori_bb", "Kalori Batubara", 80);
	    kalori_bb.setAlignment(HorizontalAlignment.RIGHT);
	    
	    ColumnConfig alamat_pemasok = new ColumnConfig("alamat_pemasok", "Alamat Pemasok", 120);
	    alamat_pemasok.setAlignment(HorizontalAlignment.RIGHT);
	    
	    columns.add(id);
	    columns.add(nama_pemasok);
	    columns.add(no_kontrak);
	    columns.add(lama_kontrak);
	    columns.add(tglkontrak);
	    columns.add(habis_kontrak);
	    columns.add(kalori_bb);
	    columns.add(alamat_pemasok);
	   
	    ListStore<PageBbDokBean> store = new ListStore<PageBbDokBean>();  
	    store.setMonitorChanges(true);  
	    store.add(TestData.getDok());  
	  
	    final ColumnModel cm = new ColumnModel(columns);
	    
	    grid = new Grid<PageBbDokBean>(store, cm);
	    grid.setBorders(true);  
	    
	    return grid;  
	  }  
}

